package com.sansi.pencilbook.mapper;

import com.sansi.pencilbook.entity.RecommendBook;
import org.apache.ibatis.annotations.*;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明: 推荐书籍mapper
 *
 * @author lisy31662
 */
@Mapper
public interface RecommendBookMapper {

  @Select("select r_id,user_id,book_isbn,category_id,book_img,book_name,book_publishers," +
      "book_price,book_author,book_introduction,book_time,order_by from recommend_book")
  public List<RecommendBook> findRecommendBooksAll() throws SQLException;

  @Select("select r_id,user_id,book_isbn,category_id,book_img,book_name,book_publishers," +
      "book_price,book_author,book_introduction,book_time,order_by from recommend_book " +
      "where user_id=#{userId} " +
      "order by order_by")
  public List<RecommendBook> findRecommendBooksByUser(int userId) throws SQLException;

  @Select("select r_id,user_id,book_isbn,category_id,book_img,book_name,book_publishers," +
      "book_price,book_author,book_introduction,book_time,order_by from recommend_book " +
      "where book_isbn=#{bookIsbn} and user_id=#{userId}")
  public RecommendBook findRecommendBookByUserIsbn(int userId, String bookIsbn) throws SQLException;

  @Insert("insert into recommend_book (user_id,book_isbn,category_id,book_img,book_name,book_publishers," +
      "book_price,book_author,book_introduction,book_time,order_by) " +
      "values(#{userId},#{bookIsbn},#{categoryId},#{bookImg},#{bookName},#{bookPublishers}," +
      "#{bookPrice},#{bookAuthor},#{bookIntroduction},#{bookTime},#{orderBy})")
  public int saveRecommendBook(RecommendBook recommendBook) throws SQLException;

  @Insert("<script>" +
      "insert into recommend_book (user_id,book_isbn,category_id,book_img,book_name,book_publishers," +
      "book_price,book_author,book_introduction,book_time,order_by) " +
      "values " +
      "<foreach collection='recommendBooks' item='book' index='index' separator=','>" +
      "(#{book.userId},#{book.bookIsbn},#{book.categoryId},#{book.bookImg},#{book.bookName}," +
      "#{book.bookPublishers},#{book.bookPrice},#{book.bookAuthor},#{book.bookIntroduction},#{book.bookTime},#{book.orderBy})" +
      "</foreach>" +
      "</script>")
  public int saveRecommendBooks(List<RecommendBook> recommendBooks) throws SQLException;

  @Delete("delete from recommend_book where user_id=#{userId}")
  public int deleteRecommendBooksByUser(int userId) throws SQLException;

}